from nest.tree.abstract import AbstractTree
from nest.path.exception import PathError
from nest.path.iterator import PathKey
from nest.tree.exception import TreeError


class CoveringTree(AbstractTree):

    def __init__(self, nest, value=None):
        super(CoveringTree, self).__init__(nest)
        if value is None:
            self.value = self.default_value
        else:
            self.value = value

    def cover(self, value):
        self.value = value
        return self

    def get(self, path):
        if isinstance(path, PathKey):
            key = path.get_path()
            value = self.get_item(key)
            return self.__class__(self._nest, value)
        return super(CoveringTree, self).get(path)

    def get_item(self, key):
        raise PathError()

    def set(self, path, value):
        if isinstance(path, PathKey):
            key = path.get_path()
            value = self.set_item(key, value)
            return self
        return super(CoveringTree, self).set(path, value)

    def set_item(self, key, value):
        raise TreeError('Not implemented!')
